﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MapReduce.Utils
{
    public class Logger
    {
        private static String mStatusSubDir = DateTime.Now.ToString("f").Replace(",", "-").Replace(":", "-");
        public static String GetStatus(String pSlaveAddress)
        {
            String file = GetFile(pSlaveAddress);
            return File.ReadAllText(file);
        }
        public static void Log(String pSlaveAddress, String pReq, Object pData)
        {
            String file = GetFile(pSlaveAddress);
            File.AppendAllText(file, "<b>" + DateTime.Now + "&gt;</b>" + pReq + " : "+pData+"<br/>");
        }

        public static void LogError(String pSlaveAddress, String pReq, Object pData)
        {
            Log(pSlaveAddress, pReq, "<span style='color:red'>"+pData+"</span>");
        }
        static String GetFile(String pSlaveAddress)
        {
            String logDir = U.MapPath("Logs");
            logDir = Path.Combine(logDir, "SlaveStatus");
            logDir = Path.Combine(logDir, mStatusSubDir);
            
            if (!Directory.Exists(logDir)) Directory.CreateDirectory(logDir);

            return Path.Combine(logDir, pSlaveAddress + ".html");

        }

    }
}
